package com.dbn.webmagic.pageprocessor;

import com.alibaba.fastjson.JSONObject;
import com.dbn.webmagic.dao.*;
import com.dbn.webmagic.utils.UrlDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liheping
 * @Description 农业农村部科技发展中心数据爬取
 * @Date 2021/11/22 16:30
 **/
@Component
public class NoticeProcessor implements PageProcessor {
    private int flag=0;//标识，翻页爬所有url只执行一次
    @Override
    public void process(Page page) {
        if(page.getUrl().regex(UrlDetail.comnoticelist_URL).match()){
            if(flag==0){
                String count=new JsonPathSelector("$.count").select(page.getRawText());
                if(Integer.parseInt(count)==0){
                    return;
                }
                int pageNum=(Integer.parseInt(count) + 15 - 1) / 15;//总页数
                if(pageNum>1){
                    String sel2=(String) page.getRequest().getExtra("sel2");
                    String sel1=(String) page.getRequest().getExtra("sel1");
                    String name=(String) page.getRequest().getExtra("name");
                    String sel3=(String) page.getRequest().getExtra("sel3");
                    String per=(String) page.getRequest().getExtra("per");
                    for(int i=0;i<pageNum-1;i++){
                        String nexturl= UrlDetail.comfirst_URL +per+"&sel1="+sel1+"&sel2="+sel2+"&sel3="+sel3+"&sel4=&t1=&t2=&t3=&t4=&pgs=15&nid=0&name="+name+"&ad=&pgi="+(i+1)+"&_=1635923968459";;
                        page.addTargetRequest(nexturl);
                    }
                }
                flag=1;
            }
            List<String> list=new JsonPathSelector("$.data[*]").selectList(page.getRawText());
               if(list.size()==0){
                   return;
               }
            for (String json : list) {
                JSONObject jsonObject = JSONObject.parseObject(json);
                String applyno=jsonObject.getString("applyno");
                String t=jsonObject.getString("t");
                String id=jsonObject.getString("id");
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("noticesimpleid",id);
                params.put("applyno",applyno);
                Request comRequest=new Request(UrlDetail.comnotice_URL+applyno);
                comRequest.setMethod(HttpConstant.Method.GET);
                //用于网页之间传参
                comRequest.setExtras(params);
                comRequest.setRequestBody(HttpRequestBody.form(params, "utf-8"));
                page.addTargetRequest(comRequest);
                //page里添加公告url
                Request noticeRequest=new Request(UrlDetail.notice_URL+t+"&an="+id+"");
                noticeRequest.setMethod(HttpConstant.Method.GET);
                //用于网页之间传参
                noticeRequest.setExtras(params);
                noticeRequest.setRequestBody(HttpRequestBody.form(params, "utf-8"));
                page.addTargetRequest(noticeRequest);
            }
        }
        if(page.getUrl().toString().contains(UrlDetail.comnotice_URL)){
            String noticesimpleid=(String) page.getRequest().getExtra("noticesimpleid");
            String applyno=(String) page.getRequest().getExtra("applyno");
            page.putField("noticesimpleid",noticesimpleid);
            page.putField("applyno",applyno);
            page.putField("comdocument",page.getHtml().getDocument());
        }

        if(page.getUrl().toString().contains(UrlDetail.notice_URL)){
            String noticesimpleid=(String) page.getRequest().getExtra("noticesimpleid");//公告唯一id
            String applyno=(String) page.getRequest().getExtra("applyno");//申请号
            page.putField("noticesimpleid",noticesimpleid);
            page.putField("applyno",applyno);
            page.putField("noticedocument",page.getHtml().getDocument());
        }
        //用作爬取植物名
//        if(page.getUrl().toString().contains(plant_url)){
//            List<String> list=new JsonPathSelector("$.*").selectList(page.getRawText());
//            for (String json : list) {
//                PlantName plantName=new PlantName();
//                JSONObject jsonObject = JSONObject.parseObject(json);
//                String lname=jsonObject.getString("lname");
//                String name=jsonObject.getString("name");
//                plantName.setLname(lname);
//                plantName.setName(name);
//                plantNameDao.insert(plantName);
//            }
//        }

    }

    @Override
    public Site getSite() {
        return Site.me().setSleepTime(100);
    }
}
